﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace FileProtector
{
    public partial class frmProtectSingle : Form
    {
        //Settings
        Libraries.SettingParams sp;

        //Temp
        string tmp = System.IO.Path.GetDirectoryName(Application.ExecutablePath) + "\\temp";

        public frmProtectSingle()
        {
            InitializeComponent();

            //Settings Generator
            sp = new Libraries.SettingParams(System.IO.Path.GetDirectoryName(Application.ExecutablePath));
            //Create Temp
            if (!Directory.Exists(tmp))
                Directory.CreateDirectory(tmp);
        }

        //Open File to Encrypt
        private void btnBrowse_Click(object sender, EventArgs e)
        {
            ofd.Filter = "All files|*.*";
            if (ofd.ShowDialog() == DialogResult.OK)
                txtEncFile.Text = ofd.FileName;
        }

        //Encrypt File
        private void btnEncrypt_Click(object sender, EventArgs e)
        {
            try
            {
                //Validate if any file is selected
                if (string.IsNullOrEmpty(txtEncFile.Text))
                {
                    MessageBox.Show("You haven't selected any file to encrypt", "!Error");
                    return;
                }

                //Check passwords
                if (string.IsNullOrEmpty(txtEncPassword.Text))
                {
                    MessageBox.Show("Please enter a password", "!Error");
                    return;
                }

                if (txtEncPassword.Text != txtEncConfirmPassword.Text)
                {
                    MessageBox.Show("Password and confirm password is not matching", "!Error");
                    return;
                }

                //Encrypt now
                sp.EncryptSingle(txtEncFile.Text, tmp);

                //Show Confirmation
                MessageBox.Show("File encrypted", "Success");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "!System Error");
            }
            finally
            {
                txtEncFile.Text = "";
                txtEncPassword.Text = "";
                txtEncConfirmPassword.Text = "";
            }
        }

        //Open file to decrypt
        private void btnBrowseEncryptedFile_Click(object sender, EventArgs e)
        {
            ofd.Filter = "All files|*.*";
            if (ofd.ShowDialog() == DialogResult.OK)
                txtDecFile.Text = ofd.FileName;
        }

        //Decrypt file
        private void btnDecrypt_Click(object sender, EventArgs e)
        {
            try
            {
                //Validate if any file is selected
                if (string.IsNullOrEmpty(txtDecFile.Text))
                {
                    MessageBox.Show("You haven't selected any file to encrypt", "!Error");
                    return;
                }

                //Check passwords
                if (string.IsNullOrEmpty(txtDecPassword.Text))
                {
                    MessageBox.Show("Please enter a password", "!Error");
                    return;
                }

                if (txtDecPassword.Text != txtDecConfirmPassword.Text)
                {
                    MessageBox.Show("Password and confirm password is not matching", "!Error");
                    return;
                }

                //Encrypt now
                sp.DecryptSingle(txtDecFile.Text, tmp);

                //Show Confirmation
                MessageBox.Show("File decrypted", "Success");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "!System Error");
            }
            finally
            {
                txtDecFile.Text = "";
                txtDecPassword.Text = "";
                txtDecConfirmPassword.Text = "";
            }
        }

        
    }
}
